package cn.zongtilei;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.isever.IUserService;
import cn.lei.User;
import cn.severimpl.UserServiceImpl;

/**
 * Servlet implementation class LoginServlet2
 */
public class LoginServlet2 extends HttpServlet {
	private IUserService userservice = new UserServiceImpl();
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.获得参数
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		if (name==""||password=="") {
			resp.sendRedirect(req.getContextPath()+"/fail2.html");
			return;
		}
		List<User> userlist = userservice.findByUser(name, password);
		for (User user : userlist) {
			if (user.getName().equals(name)&&user.getPassword().equals(password)) {
				//登陆成功
				//创建Session
				HttpSession httpSession = req.getSession();
				//把数据保存到session中
				httpSession.setAttribute("user", user);
				resp.sendRedirect(req.getContextPath()+"/student?method=pageList");
				return;
			}
		}
		resp.sendRedirect(req.getContextPath()+"/fail.html");
		/**
		 * 这个开始做错了，原因是当时在dao里面返回的是一个对象，没有进行遍历，导致一个值跟一个集合进行比较
		 * 所以当数据库只有一条sql语句可以执行，多了就500.
		 */
		
		//2.业务处理
		//User user = userservice.findByUser(name, password);
		/*if (user.getName().equals(name)&&user.getPassword().equals(password)) {
			//登陆成功
			//创建Session
			HttpSession httpSession = req.getSession();
			//把数据保存到session中
			httpSession.setAttribute("user", user);
			resp.sendRedirect(req.getContextPath()+"/student?method=pageList");
		}else{
			resp.sendRedirect(req.getContextPath()+"/fail.html");
		}
	}*/

}
}
